﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ProgramMain.Database;
using ProgramMain.Database.SimpleOpticNetDBTableAdapters;
using ProgramMain.Google;

namespace ProgramMain.Layers
{
    public class CableTunnelNode : IGoogleTreeNode
    {
        private SimpleOpticNetDB.CableTunnelsRow _Row;

        public GoogleTreeTypes NodeType
        {
            get { return GoogleTreeTypes.Line; }
        }

        public Coordinate Coordinate
        {
            get { throw new NotImplementedException(); }
        }

        public CoordinateRectangle Rectangle
        {
            get
            {
                return new CoordinateRectangle(
                    _Row.Longitude1,
                    _Row.Latitude1,
                    _Row.Longitude2,
                    _Row.Latitude2);
            }
        }

        public int RowID
        {
            get { return _Row.ID; }
        }

        internal CableTunnelNode(SimpleOpticNetDB.CableTunnelsRow row)
        {
            _Row = row;    
        }

        public DataRow Row
        {
            get { return _Row; }
        }
    }

    public class CableTunnelTree : GoogleTree<CableTunnelNode>
    {
        public SimpleOpticNetDB.CableTunnelsDataTable cableTunnelDBRows { get; private set; }

        public CableTunnelTree()
            : base(LevelPowerTypes.Ultra, LevelPowerTypes.Extra, LevelPowerTypes.Medium, LevelPowerTypes.Low)
        {
        }

        public void Insert(SimpleOpticNetDB.CableTunnelsRow row)
        {
            base.Insert(new CableTunnelNode(row));
        }

        public void Delete(SimpleOpticNetDB.CableTunnelsRow row)
        {
            base.Delete(new CableTunnelNode(row));
        }
        
        public void LoadData()
        {
            Clear();

            CableTunnelsTableAdapter adapter = new CableTunnelsTableAdapter();
            adapter.ClearBeforeFill = true;
            adapter.SelectCommandTimeout = Properties.Settings.Default.LongQueryTimeout;

            cableTunnelDBRows = adapter.GetData(null, null, null, null, null, null, null, null, null);

            Parallel.ForEach(cableTunnelDBRows.Cast<SimpleOpticNetDB.CableTunnelsRow>(), Insert);
        }

        public void MergeData(SimpleOpticNetDB.CableTunnelsDataTable cableTunnels)
        {
            if (cableTunnelDBRows == null) return;

            cableTunnelDBRows.Merge(cableTunnels, false, MissingSchemaAction.Error);

            Parallel.ForEach(cableTunnels.Cast<SimpleOpticNetDB.CableTunnelsRow>(), delegate(SimpleOpticNetDB.CableTunnelsRow row)
            {
                SimpleOpticNetDB.CableTunnelsRow newRow = cableTunnelDBRows.FindByID(row.ID);
                Insert(newRow);
            });
        }

        public bool RemoveCableTunnel(int ObjectID)
        {
            SimpleOpticNetDB.CableTunnelsRow row = (cableTunnelDBRows == null) ? null : cableTunnelDBRows.FindByID(ObjectID);
            if (row != null)
            {
                Delete(row);

                cableTunnelDBRows.Rows.Remove(row);

                return true;
            }
            return false;
        }

        public SimpleOpticNetDB.CableTunnelsRow FindCableTunnel(int ObjectID)
        {
            if (cableTunnelDBRows == null) return null;

            SimpleOpticNetDB.CableTunnelsRow row = cableTunnelDBRows.FindByID(ObjectID);
            if (row != null)
            {
                SimpleOpticNetDB.CableTunnelsDataTable dt = (SimpleOpticNetDB.CableTunnelsDataTable)cableTunnelDBRows.Clone();
                dt.ImportRow(row);
                return (SimpleOpticNetDB.CableTunnelsRow)dt.Rows[0];
            }
            return null;
        }
    }
}
